package com.hexb.smh.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.hexb.smh.entity.enums.ConfigType;
import com.hexb.smh.utils.Converters;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;

/**
 * @author : hexb
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class EvaluateLevel {

    static private final DecimalFormat f = new DecimalFormat("0.#");

    private Integer id;

    @ApiModelProperty("等级名称,如-A,-B...+A,+B,AA")
    private String levelName;

    @ApiModelProperty("评分段开始值")
    private BigDecimal valueFrom;

    @ApiModelProperty("评分段结束值")
    private BigDecimal valueTo;

    @ApiModelProperty("评分是否达标")
    private Boolean passed;

    @ApiModelProperty("创建时间")
    @JsonFormat(
            pattern = "yyyy-MM-dd HH:mm:ss",
            timezone = "GMT+8"
    )
    private Date createTime;

    @ApiModelProperty("最后一次修改时间")
    @JsonFormat(
            pattern = "yyyy-MM-dd HH:mm:ss",
            timezone = "GMT+8"
    )
    private Date updateTime;

    @ApiModelProperty("创建人")
    private Integer createBy;

    @ApiModelProperty("最后一次修改人")
    private Integer updateBy;

    static private String format(BigDecimal b) {
        if (null == b) return null;
        return f.format(b);
    }

    public String getLeftValue() {
        if (null != valueTo && null != valueFrom) {
            if (valueFrom.abs().compareTo(valueTo.abs()) < 0) {
                return format(valueFrom);
            } else {
                return format(valueTo);
            }
        }
        if (null != valueFrom) {
            return format(valueFrom);
        } else {
            return format(valueTo);
        }
    }

    public String getRightValue() {
        if (null != valueTo && null != valueFrom) {
            if (valueFrom.abs().compareTo(valueTo.abs()) > 0) {
                return format(valueFrom);
            } else {
                return format(valueTo);
            }
        }
        return null;
    }

    public String format() {
        return String.format("[%s](%s-%s)", levelName, getLeftValue(), getRightValue());
    }

}
